# .onAttach <- function(libname, pkgname ){
#   library(leaflet)
#   library(sp)
# }
globalVariables(c("leafletcn.map.names", ".triList"))

## Encoding solution
encodingSolution = function(str){
  iconv(str, "UTF-8", "UTF-8")
}

## read function
readGeoLocal = function(city){
  query = toLabel(city)
  if(!any(leafletcn.map.names$label==query)){
    stop(paste0("\n",
                city,
                ": this mapType cannot found!\n",
                "Please check the mapType name or use icnov to convert encoding.\n",
                "Valid mapTypes: regionNames()\n",
                "Encoding convert: ?iconv"))
  }
  file = paste0("geojson/",
                leafletcn.map.names$files[leafletcn.map.names$label==query]
  )
  filePath = system.file(file,package = "leafletCN")

  # output = rgdal::readOGR(filePath, "OGRGeoJSON")
  output = read.geoShape(filePath)
  if(.Platform$OS.type == "windows"){
    output$name = encodingSolution(output$name)
  }

  return(output)
}

## .triList
## Use first two words to match
toLabel = function(city){
  labels = sapply(city, function(x){
    if(tolower(substr(x,1,1)) %in% letters){
      return(tolower(x))
    }else if(x == .triList[[5]] | grepl(paste0(.triList[[5]],.triList[[7]][1]), x)){
      warning("Using Jilin Province instead of Jilin City!")
      return(.triList[[5]])
    } else if(grepl(.triList[[5]], x) & !grepl(paste0(.triList[[5]],.triList[[7]][1]), x)){
      return(paste0(.triList[[5]],.triList[[7]][2]))
    }
    else if(x == .triList[[6]] | grepl(paste0(.triList[[6]],.triList[[7]][1]), x)){
      warning("Using Hainan Province instead of Hainan City!")
      return(.triList[[6]])
    }else if(grepl(.triList[[6]], x) & !grepl(paste0(.triList[[6]],.triList[[7]][1]), x)){
      return(paste0(.triList[[6]],.triList[[7]][2]))
    }else if(grepl(.triList[[1]], x)|
       grepl(.triList[[2]], x)|
       grepl(.triList[[3]], x)|
       grepl(.triList[[4]], x)
    ){
      return(substr(x, 1, 3))
    }else{
      return(substr(x, 1, 2))
    }
  })
  return(labels)
}

## Fork from echarts
evalFormula = function(x, data) {
  # x = ~value; data = mapData
  if (!inherits(x, 'formula')) return(x)
  if (length(x) != 2) stop('The formula must be one-sided: ', deparse(x))
  x_formula = terms.formula(x)
  if (length(attr(x_formula, "term.labels")) == 1){
    eval(x[[2]], data, environment(x))
  }else{
    as.data.frame(sapply(attr(x_formula, "term.labels"),function(tmpTerm){
      return(eval(as.name(tmpTerm), data, environment(x)))
    }),stringsAsFactors=F)
  }
}
